package com.calsoft.Hops_Mobile_Tablet.sql;
/*
 * User: Minh Diep
 * Date: 7/3/13
 * Time: 1:01 PM
 */

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import com.calsoft.Hops_Mobile_Tablet.base.CardUIData;

import java.io.IOException;

public class ProfileScheduleDbAdapter {

    private static final String TABLE = "profileSchedule";
    public static final String ID = "_id";
    public static final String CARD_DATE = "CardDate";
    public static final String CARD_TYPE = "CardType";
    public static final String CARD_NAME = "CardName";
    public static final String CARD_START_TIME = "CardStartTime";
    public static final String CARD_END_TIME = "CardEndTime";
    public static final String CARD_DESC = "CardDesc";
    public static final String ITEM_ID = "ItemID";
    public static final String IMAGE_TAG = "ImageTag";

    public static final String[] selectedCols = {ID, CARD_TYPE, CARD_NAME, CARD_START_TIME, CARD_END_TIME, CARD_DESC, ITEM_ID, IMAGE_TAG};

    private DataBaseHelper mDbHelper;
    private Context context;

    public ProfileScheduleDbAdapter (Context context){
        this.context = context;
    }

    public void open() throws SQLException {
        mDbHelper = new DataBaseHelper(context);
        try {
            mDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            mDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }
    }

    public void close() {
        mDbHelper.close();
    }

    public long insertCardUIData(CardUIData cardUIData, String date){
        ContentValues values = new ContentValues();
        values.put(ID, cardUIData.getId());
        values.put(CARD_DATE, date);
        values.put(CARD_TYPE, cardUIData.getType());
        values.put(CARD_NAME, cardUIData.getName());
        values.put(CARD_START_TIME, cardUIData.getStartTime());
        values.put(CARD_END_TIME, cardUIData.getEndTime());
        values.put(CARD_DESC, cardUIData.getCardDesc());
        values.put(ITEM_ID, cardUIData.getItemId());
        values.put(IMAGE_TAG, cardUIData.getImageTag());
        return mDbHelper.insert(TABLE, null, values);
    }

    public Cursor fetchAllScheduleByDate(String date){
        return mDbHelper.queryRow(TABLE, selectedCols, CARD_DATE + "=\"" + date + "\"", CARD_START_TIME);
    }

    public long numberOfRows(){
        return mDbHelper.numberOfRows(TABLE);
    }

}
